package cn.yzy.manager;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;
import javax.swing.text.rtf.RTFEditorKit;

import org.hibernate.Query;
import org.hibernate.Session;

import cn.yzy.db.Group;
import cn.yzy.db.HibernateUtil;
import cn.yzy.db.Keyword;
import cn.yzy.db.KnowledgePoint;
import cn.yzy.ui.AddItemPanel_bean;

public class SaveUtil {

	public static KnowledgePoint convertBeanToKP(AddItemPanel_bean bean) {
		KnowledgePoint kp=new KnowledgePoint(bean.getTitle());
		kp.setGroup(new Group(bean.getCurrentGroup()));
		Set<Keyword> keySet = new HashSet<Keyword>();
		for(String keyName:bean.getKeywordsList()) {
			keySet.add(new Keyword(keyName));
		}
		kp.setKeywords(keySet);
		kp.setContent(SaveUtil.docToByteArray(bean.getCurrentDoc()));
		return kp;
	}

	
	
	public static void saveDocToDB(AddItemPanel_bean bean) {
		KnowledgePointManager.importKP(SaveUtil.convertBeanToKP(bean));
	}

	private Keyword findKeywordByName(Session s, String key) {
		String sql = "select k from Keyword k where k.name=:keyName";
		Query q = s.createQuery(sql);
		q.setString("keyName", key);
		if (q.list().size() > 0) {
			return (Keyword) q.list().get(0);
		} else {
			return null;
		}
	}

	private Group findGroupByName(Session s, String groupName) {
		String sql = "select g from Group g where g.name=:groupName";
		Query q = s.createQuery(sql);
		q.setString("groupName", groupName);
		if (q.list().size() > 0) {
			return (Group) q.list().get(0);
		} else {
			return null;
		}
	}

//	private MyDoc findDocByTitle(Session s, String title) {
//		String sql = "select doc from MyDoc doc where doc.title=:title";
//		Query q = s.createQuery(sql);
//		q.setString("title", title);
//		if (q.list().size() > 0) {
//			return (MyDoc) q.list().get(0);
//		} else {
//			return null;
//		}
//	}

	public static  byte[] docToByteArray(StyledDocument document) {
		byte[] data = null;
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		RTFEditorKit kit = new RTFEditorKit();
		try {
			kit.write(bos, document, 0, document.getLength());
		} catch (IOException e) {
			e.printStackTrace();
		} catch (BadLocationException e) {
			e.printStackTrace();
		}

		data = bos.toByteArray();
		return data;
	}

//	public static boolean validateTitle(String title) {
//		Session s = HibernateUtil.currentSession();
//		s.beginTransaction();
//		boolean result = true;
//		String sql = "select doc from MyDoc doc where doc.title=:title";
//		Query q = s.createQuery(sql);
//		q.setString("title", title);
//		if (q.list().size() > 0) {
//			result = false;
//		}
//		s.getTransaction().commit();
//		return result;
//	}

	public static StyledDocument byteArrayToDoc(byte[] content,
			StyledDocument document) {
		ByteArrayInputStream bis = new ByteArrayInputStream(content);
		RTFEditorKit kit = new RTFEditorKit();
		try {
			kit.read(bis, document, 0);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (BadLocationException e) {
			e.printStackTrace();
		}

		return document;
	}

}
